﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace UIMaker
{
    public partial class UIMakerForm : Form
    {
        UIPanel uiPanel;
        XMenu xmenuInstance = new XMenu();
        protected XControl selectedControl;
        public XScreen selectedScreen;
        public int screenWidth = 480;
        public int screenHeight = 320;
        public XControl SelectedControl
        {
            get { return selectedControl; }
            set 
            {
                selectedControl = value;
                propertyGrid.SelectedObject = selectedControl;
            }
        }
        public UIMakerForm()
        {
            InitializeComponent();
            try
            {
                TextReader reader = new StreamReader("screen_size.txt");
                do
                {
                    String value = reader.ReadLine();
                    if (value == null) break;
                    comboBoxScreenSize.Items.Add(value);
                }
                while (true);
            }
            catch (IOException ex)
            {
            }
            comboBoxScreenSize.SelectedIndex = 0;
        }

        private void UIMakerForm_Load(object sender, EventArgs e)
        {
            uiPanel = new UIPanel(this);
            //XControl xControl = new XScreen();
            //propertyGrid.SelectedObject = xControl;
            uiPanel.Location = new Point(0, toolStripUIPanel.Height);
            uiPanel.Size = new System.Drawing.Size(splitContainerMain.Panel2.ClientSize.Width, splitContainerMain.Panel2.ClientSize.Height - toolStripUIPanel.Height);
            splitContainerMain.Panel2.Controls.Add(uiPanel);
        }

        private void buttonAddScreen_Click(object sender, EventArgs e)
        {
            SpriteMaker.TextInput dialog = new SpriteMaker.TextInput("New Screen", "Please type a name");
            dialog.ShowDialog();
            if (dialog.Value != "")
            {
                XScreen screen = xmenuInstance.AddScreen(dialog.Value);
                screen.SizeAbsolute = new SizeF(screenWidth, screenHeight);
                XTreeNode node = new XTreeNode(dialog.Value, screen);
                treeViewMenu.Nodes.Add(node);
                if (selectedScreen == null)
                {
                    if (treeViewMenu.SelectedNode == null)
                    {
                        treeViewMenu.SelectedNode = node;
                    }
                }
            }
        }

        private void treeViewMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                 selectedScreen = (XScreen)((XTreeNode)e.Node).xcontrol;
            }
            SelectedControl = ((XTreeNode)e.Node).xcontrol;
            uiPanel.Invalidate();
        }

        private void buttonAddButton_Click(object sender, EventArgs e)
        {
            if (treeViewMenu.SelectedNode != null && SelectedControl != null)
            {
                if (SelectedControl.IsContainer())
                {
                    XButton button = new XButton();
                    ((XContainer)SelectedControl).AddChild(button);
                    treeViewMenu.SelectedNode.Nodes.Add(new XTreeNode("Button", button));
                    uiPanel.Invalidate();
                }
            }
        }

        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            uiPanel.Invalidate();
        }

        private void comboBoxScreenSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String[] splitted = comboBoxScreenSize.Items[comboBoxScreenSize.SelectedIndex].ToString().Split('x');
                screenWidth = Convert.ToInt32(splitted[0]);
                screenHeight = Convert.ToInt32(splitted[1]);
                if (selectedScreen != null)
                {
                    selectedScreen.SizeAbsolute = new SizeF(screenWidth, screenHeight);
                    uiPanel.Invalidate();
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void buttonAddDialog_Click(object sender, EventArgs e)
        {
            if (treeViewMenu.SelectedNode != null && SelectedControl != null)
            {
                if (SelectedControl.IsContainer())
                {
                    XDialog dialog = new XDialog();
                    ((XContainer)SelectedControl).AddChild(dialog);
                    treeViewMenu.SelectedNode.Nodes.Add(new XTreeNode("Dialog", dialog));
                    uiPanel.Invalidate();
                }
            }
        }
    }
}
